﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media.Effects;

namespace RadiantQ.Windows.Controls
{
    /// <summary>
    /// The visual that represents the expanded/collapsed states of parent activities.
    /// </summary>
    ///<compilewhen>never</compilewhen>
    public class ExpanderCue : Control
    {
        /// <summary>
        /// Specifies if user can click on this visual.
        /// </summary>
        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        /// <summary>
        /// Identifies the <see cref="P:ExpanderCue.IsReadOnly">IsReadOnly</see>
        /// dependency property.
        /// </summary>
        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(ExpanderCue), new PropertyMetadata(false));


        /// <summary>
        /// Specifies if transition animation should be used when changing state.
        /// </summary>
        public bool UseTransitions { get; set; }
        #region Dependency Properties
        /// <summary>
        /// Identifies the <see cref="P:ExpanderCue.IsExpanded">IsExpanded</see>
        /// dependency property.
        /// </summary>
        public static DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(ExpanderCue), new PropertyMetadata(false, new PropertyChangedCallback(ExpanderCue.IsExpandedChangedHandler)));
        /// <summary>
        /// Specifies if this visual should be rendered in expanded state.
        /// </summary>
        public bool IsExpanded
        {
            get { return (bool)GetValue(IsExpandedProperty); }
            set
            {
                SetValue(IsExpandedProperty, value);
            }
        }
        #endregion

        #region Template Parts

        /// <summary>
        /// The transform that defines the rotation in the template.
        /// </summary>
        protected RotateTransform ExpanderRotateTranform { get; private set; }

        #endregion


        #region Events

        static void IsExpandedChangedHandler(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ExpanderCue expander = obj as ExpanderCue;

            expander.SetVisualState(true);
        }

        #endregion

        #region Constructors and overrides
        /// <summary>
        /// Constructor.
        /// </summary>
        public ExpanderCue()
        {
            this.DefaultStyleKey = typeof(ExpanderCue);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(SimpleExpander_MouseLeftButtonDown);
            this.UseTransitions = false;
        }

        // Highlight
        /// <summary>
        /// Overridden to listen to mouse enter.
        /// </summary>
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            if(!this.IsReadOnly)
                this.Effect = new DropShadowEffect() { Color = Colors.Black, Direction = 320, ShadowDepth = 2, BlurRadius = 5, Opacity = 0.75 };
        }
        /// <summary>
        /// Overridden to listen to mouse leave.
        /// </summary>
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            if (!this.IsReadOnly)
                this.Effect = null;
        }
        /// <summary>
        /// Overridden to refer to certain specific named elements in the template.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            ExpanderRotateTranform = (GetTemplateChild("Triangle") as Polygon).RenderTransform as RotateTransform;

            this.SetVisualState(false);
            this.UseTransitions = true;
        }
        
        #endregion

        #region Private functions
        private void SetVisualState(bool useTransitions)
        {
            useTransitions = useTransitions && this.UseTransitions;
            if (IsExpanded)
            {
                VisualStateManager.GoToState(this, "Expanded", useTransitions);
            }
            else
            {
                VisualStateManager.GoToState(this, "Collapsed", useTransitions);
            }
        }
        #endregion

        #region Event handling function
        
        private void SimpleExpander_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!this.IsReadOnly)
            {
                IsExpanded = !IsExpanded;
                e.Handled = true;
            }
        }

        #endregion
    }
}
